<?php
namespace Home\Controller;
use Think\Controller;

class SuanhuaController extends Controller {
    public function index()
    {
        //接口地址
        $url="https://test.suanhua.org/antifraud/api/v4/application";  


        $_GET['name'] =  isset($_GET['name']) ? $_GET['name'] : '张三' ;
        $_GET['phone'] =  isset($_GET['phone']) ? $_GET['phone'] : '18195426554' ;
        $_GET['date'] =  isset($_GET['date']) ? $_GET['date'] : '20180212151304';
        $_GET['idcard_num'] =  isset($_GET['idcard_num'])?  $_GET['idcard_num'] : '450803198710067035';

       

        //
        $key       = C('key');        //机构私钥
        $TIMESTAMP = $this->getMillisecond();   //毫秒时间戳
        $NONCE     = '1234567890';              //任意字符串

        $str = $NONCE.$TIMESTAMP.$key;
        $key = md5($str);

        $data = '{
                    "TIMESTAMP": "'.$TIMESTAMP.'",
                    "NONCE": "'.$NONCE.'",
                    "SIGN": "'.$key.'",
                    "ORG_NUM": "OG01000942",
                    "DATA": {
                        "APP_NUM":"'.md5(time()).'",
                        "APP_DATE":"'.$_GET['date'].'",

                        "FRD_DATA_NUM": "1",
                        "ID_TYPE": "0",
                        "FRD_PRODUCT_NUM": "AFP1002,AFP1011",
                        "APP_PRODUCT_NUM": "A01",
                        "TRANSFER_ORG_NUM":"TRA000",
                         "CUST_NAME": "'.$_GET['name'].'",
                        "CELL_PHONE": "'.$_GET['phone'].'",
                        "ID_NUM": "'.$_GET['idcard_num'].'"
                    }
                }';
                
        //curl请求
        $ch = curl_init();
        $header = array();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json;charset=utf-8','Accept:application/json'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        
       
            
        $jsonstr = json_decode($data);
            // echo '<pre>';
        $jsonstr = $jsonstr->DATA;
        //  print_r($jsonstr);
         $this->assign('data',$jsonstr);
         $this->assign('name',$_GET['name']);
        echo $this->fetch(index);
       
    }


    public function getMillisecond() { 
        list($t1, $t2) = explode(' ', microtime()); 
        return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000); 
    }


    function zidian($sign){

        $sign = str_split($sign);
        $num = [];

        foreach($sign as $k=>$v){
            
            //拿出数字
            if(is_numeric($v)){
                $num[] = $v;
                unset($sign[$k]);
            }
        }
        sort($num);
        sort($sign);
        $num = implode('',$num);
        $sign = implode('',$sign);
        $sign = $num.$sign;
        return $sign;
    } 

    public function pdf(){

$input = new UrlInput();
$input->setUrl('https://www.baidu.com');

$converter = new Converter($input, new DownloadOutput());

$converter->setOption('n');
$converter->setOption('d', '300');

$converter->setOptions([
    'no-background',
    'margin-bottom' => '100',
    'margin-top' => '100',
]);

$output = $converter->convert();
$output->download();

    }
}